import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class SuperLearn {
	static Random rnd = new Random(1);

	static double[][] weights = new double[3][Tilecoder.numOfTilings*Tilecoder.DIFF_BETWEEN_RUNS];
	static double alpha = .05/Tilecoder.numOfTilings;
	

	public static double f(int action,double[] state) {
		int [] tiles = new int[Tilecoder.numOfTilings];
		double f_of_x =0;
		Tilecoder.tilecode(state[0],state[1],tiles);
		for(int i=0;i<Tilecoder.numOfTilings;i++){
			f_of_x += weights[action][tiles[i]];
		}
		return f_of_x;
	}

	public static void learn(int action,double [] state,double[] nextState,double y) {
		int [] tiles = new int[Tilecoder.numOfTilings];
		Tilecoder.tilecode(state[0],state[1],tiles);
		for(int i=0;i<Tilecoder.numOfTilings;i++){
			weights[action][tiles[i]]+= alpha*(-1+f(action,nextState)-f(action,state));
		}
	}
	
	public static void learn(int action, double[] state, double sigma){
		int[] tiles = new int[Tilecoder.numOfTilings];
		Tilecoder.tilecode(state[0],state[1],tiles);
		for(int i=0;i<Tilecoder.numOfTilings;i++){
			weights[action][tiles[i]]+= alpha * sigma;
		}
	}
	public static void reset(){
		for(int j=0;j<3;j++){
			for(int i=0;i<(Tilecoder.numOfTilings*Tilecoder.DIFF_BETWEEN_RUNS);i++){
				weights[j][i] = -Math.random()/100;
			}
		}
	}

	public static void writeF(String filename) {
		FileWriter fstreamout;
		try {
			fstreamout = new FileWriter(filename);
			BufferedWriter out = new BufferedWriter(fstreamout);
			int x1steps = 50, x2steps = 50;
			double[] state = new double[2];
			state[1]= Tilecoder.MIN_V;
			for (int i = 0; i < x1steps; i++) {
				state[0]= Tilecoder.MIN_X;
				for (int j = 0; j < x2steps; j++){
					double y=-f(0,state);
					for(int k = 1;k<3;k++){
						y = Math.min(-f(k,state),y);
					}
					out.write(y + ",");
					state[0] += Tilecoder.RANGE_X/x1steps;
				}
				state[1] += Tilecoder.RANGE_V/x2steps;
				out.write("\n");
			}
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
